package com.eryos.android.cigarettecounter.ui;

/**
 * Project :  CigaretteCounter
 * Author :   Olivier Combe 
 * URL :      http://eryos.fr/CigaretteCounter/
 *
 * Copyright (C) 2011 - Olivier Combe 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

import java.io.IOException;
import java.net.UnknownHostException;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.Toast;

import com.eryos.android.cigarettecounter.R;
import com.eryos.android.cigarettecounter.charts.Chart;

public class ChartPopup extends Dialog{

	private ImageView chartFullScreen;
	private Context context;
	
	public ChartPopup(Context context) {
		super(context);
		this.context = context;
	}
	
	public ChartPopup(Context context, String title, String chartURL) {
		this(context);
		
		this.setContentView(R.layout.chart_dialog);
		this.setTitle(title);

		chartFullScreen = (ImageView) findViewById(R.id.popup_image);		
    	
		new DownloadFilesTask().execute(chartURL);
		/*chartFullScreen.setImageDrawable(context.getResources().getDrawable(R.drawable.eryos));
    	Log.i("Chart URL", chartURL);
		Bitmap bmp = Chart.downloadChartAsBitmap(this.getContext(), chartURL);
		chartFullScreen.setImageBitmap(bmp);
		*/
		this.show();
		
		chartFullScreen.setOnTouchListener(new OnTouchListener() {
  			public boolean onTouch(View v, MotionEvent event) {
				dismiss();
				return false;
			}
        });
	}	

	

	private class DownloadFilesTask extends AsyncTask<String, Integer, Bitmap> { 
	     
		Bitmap defaultBitmap = null;
		Bitmap errorBitmap = null;
		
		/** 
		* Step 1 - invoked on the UI thread immediately after the task is executed. 
		* This step is normally used to setup the task, for instance by showing a progress bar in the user interface 
		*/
		protected void onPreExecute() { 
			chartFullScreen.setImageDrawable(context.getResources().getDrawable(R.drawable.eryos));
		} 

		/** 
		* Step 2 - invoked on the background thread immediately after onPreExecute() finishes executing. 
		* This step is used to perform background computation that can take a long time 
		*/
		protected Bitmap  doInBackground(String... urls) { 
			
			try {
				return Chart.downloadChartAsBitmap(urls[0]);
			} catch (UnknownHostException e) {
				Toast.makeText(context, "Internet connection not available, please retry later ...", Toast.LENGTH_LONG);
				return errorBitmap;
			} catch (IOException e) {
				Toast.makeText(context, "Sorry, an error occurs while downloading chart", Toast.LENGTH_LONG);
				Log.e(getClass().getSimpleName(), "doInBackground Error ", e);
				return errorBitmap;
			}
			 
		} 
		
		/** 
		* Step 3 - invoked on the UI thread after the background computation finishes. 
		* The result of the background computation is passed to this step as a parameter. 
		*/
		protected void onPostExecute(Bitmap  result) {
			if ( result != null )
				chartFullScreen.setImageBitmap(result);
			else
				chartFullScreen.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_menu_discard));

			Toast.makeText(context, "download OK", Toast.LENGTH_SHORT);
		}
	}
}
